package org.deadline.model.baseapp;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import org.deadline.model.config.EnumerationDataOnDemand;
import org.deadline.model.user.Person;
import org.deadline.model.user.PersonDataOnDemand;
import org.deadline.repository.baseapp.ApplicationLogRepository;
import org.deadline.service.baseapp.ApplicationLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Component;

@Component
@Configurable
public class ApplicationLogDataOnDemand {

	private Random rnd = new SecureRandom();

	private List<ApplicationLog> data;

	@Autowired
    private EnumerationDataOnDemand enumerationDataOnDemand;

	@Autowired
    private PersonDataOnDemand personDataOnDemand;

	@Autowired
    ApplicationLogService applicationLogService;

	@Autowired
    ApplicationLogRepository applicationLogRepository;

	public ApplicationLog getNewTransientApplicationLog(int index) {
        ApplicationLog obj = new ApplicationLog();
        setCreationDate(obj, index);
        setDeleted(obj, index);
        setDescription(obj, index);
        setDestinationUsers(obj, index);
        setExternalClientIp(obj, index);
        setInternalIp(obj, index);
        setModificationDate(obj, index);
        setPerson(obj, index);
        setTargetUsers(obj, index);
        return obj;
    }

	public void setCreationDate(ApplicationLog obj, int index) {
        Date creationDate = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setCreationDate(creationDate);
    }

	public void setDeleted(ApplicationLog obj, int index) {
        Boolean deleted = true;
        obj.setDeleted(deleted);
    }

	public void setDescription(ApplicationLog obj, int index) {
        String description = "description_" + index;
        if (description.length() > 255) {
            description = description.substring(0, 255);
        }
        obj.setDescription(description);
    }

	public void setDestinationUsers(ApplicationLog obj, int index) {
        String destinationUsers = "destinationUsers_" + index;
        obj.setDestinationUsers(destinationUsers);
    }

	public void setExternalClientIp(ApplicationLog obj, int index) {
        String externalClientIp = "externalClientIp_" + index;
        if (externalClientIp.length() > 30) {
            externalClientIp = externalClientIp.substring(0, 30);
        }
        obj.setExternalClientIp(externalClientIp);
    }

	public void setInternalIp(ApplicationLog obj, int index) {
        String internalIp = "internalIp_" + index;
        if (internalIp.length() > 30) {
            internalIp = internalIp.substring(0, 30);
        }
        obj.setInternalIp(internalIp);
    }

	public void setModificationDate(ApplicationLog obj, int index) {
        Date modificationDate = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setModificationDate(modificationDate);
    }

	public void setPerson(ApplicationLog obj, int index) {
        Person person = personDataOnDemand.getRandomPerson();
        obj.setPerson(person);
    }

	public void setTargetUsers(ApplicationLog obj, int index) {
        String targetUsers = "targetUsers_" + index;
        obj.setTargetUsers(targetUsers);
    }

	public ApplicationLog getSpecificApplicationLog(int index) {
        init();
        if (index < 0) {
            index = 0;
        }
        if (index > (data.size() - 1)) {
            index = data.size() - 1;
        }
        ApplicationLog obj = data.get(index);
        Long id = obj.getId();
        return applicationLogService.findApplicationLog(id);
    }

	public ApplicationLog getRandomApplicationLog() {
        init();
        ApplicationLog obj = data.get(rnd.nextInt(data.size()));
        Long id = obj.getId();
        return applicationLogService.findApplicationLog(id);
    }

	public boolean modifyApplicationLog(ApplicationLog obj) {
        return false;
    }

	public void init() {
        int from = 0;
        int to = 10;
        data = applicationLogService.findApplicationLogEntries(from, to);
        if (data == null) {
            throw new IllegalStateException("Find entries implementation for 'ApplicationLog' illegally returned null");
        }
        if (!data.isEmpty()) {
            return;
        }
        
        data = new ArrayList<ApplicationLog>();
        for (int i = 0; i < 10; i++) {
            ApplicationLog obj = getNewTransientApplicationLog(i);
            try {
                applicationLogService.saveApplicationLog(obj);
            } catch (ConstraintViolationException e) {
                StringBuilder msg = new StringBuilder();
                for (Iterator<ConstraintViolation<?>> iter = e.getConstraintViolations().iterator(); iter.hasNext();) {
                    ConstraintViolation<?> cv = iter.next();
                    msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage()).append("=").append(cv.getInvalidValue()).append("]");
                }
                throw new RuntimeException(msg.toString(), e);
            }
            applicationLogRepository.flush();
            data.add(obj);
        }
    }
}
